unit UQry;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ADODB, UPub, ExtCtrls, RzPanel, StdCtrls, Mask, RzEdit, UIni,
  RzLabel, RzButton, IniFiles, ComCtrls, jpeg, DB, RzSplit, RzSpnEdt;

type
  TFQry = class(TForm)
    RzRichEdit1: TRzRichEdit;
    panel1: TRzPanel;
    Image1: TImage;
    RzLabel13: TRzLabel;
    btnQry: TRzBitBtn;
    btnCancel: TRzBitBtn;
    RzSizePanel1: TRzSizePanel;
    RzPanel1: TRzPanel;
    procedure FormCreate(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnQryClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    strSQL        : string;
    procedure Qry_Show(tabNam: string);
    procedure tboxChange(Sender: TObject);
  end;

var
  FQry          : TFQry;
  sTabNam       : string;
  search_order  : string;
  search_rows   : string;
  search_lines  : string;
  search_height : string;
  search_width  : string;
  search_left   : string;
  search_top    : string;
  search_labW   : string;
  search_tboxW  : string;
  tbox1         : TRzEdit;
  labs          : array of TRzLabel;
  tboxs         : array of TRzEdit;
  btns          : array of TRzButton;
  objs          : array of TObject;
  colTyps       : array of TFieldType;


implementation

{$R *.dfm}

procedure TFQry.Qry_Show(tabNam: string);
var
  AQry1: TADOQuery;
  colLst: TStringList;
  i: Integer;
  i_r, i_l: Integer;

begin
  //if tabNam = sTabNam then Exit;

  for i := 0 to Length(objs) - 1 do
  begin
    objs[i].Free;
    labs[i].Free;
  end;
  SetLength(objs, 0);
  SetLength(labs, 0);

  sTabNam := tabNam;
  AQry1 := TADOQuery.Create(nil);
  try
    openAQry(AQry1, 'select * from (' + tabNam + ') A');
  except
    Exit;
  end;

  search_order := IniReadStr('FQuery', 'search_order', 'row');
  search_rows  := IniReadStr('FQuery', 'search_rows',  '4');
  search_lines := IniReadStr('FQuery', 'search_lines', '4');
  search_height:= IniReadStr('FQuery', 'search_height','20');
  search_width := IniReadStr('FQuery', 'search_width', '300');
  search_left  := IniReadStr('FQuery', 'search_left',  '15');
  search_top   := IniReadStr('FQuery', 'search_top',   '10');
  search_labW  := IniReadStr('FQuery', 'search_labW',  '100');
  search_tboxW := IniReadStr('FQuery', 'search_tboxW', '80');

  i_r := -1;
  i_l := 0;
  colLst := TStringList.Create;
  AQry1.GetFieldNames(colLst);
  SetLength(colTyps, AQry1.FieldCount);
  for i := 0 to AQry1.Fields.Count - 1 do
  begin
    colTyps[i] := AQry1.Fields[i].DataType;
    AQry1.Next;
  end;
  AQry1.Free;


  SetLength(objs, colLst.Count);
  SetLength(labs, colLst.Count);
  SetLength(tboxs, colLst.Count);
  for i := 0 to colLst.Count - 1 do
  begin
    if i mod StrToInt(search_rows) = 0 then
    begin
      Inc(i_r);
      i_l := 0;
    end
    else
    begin
      Inc(i_l);
    end;

    labs[i] := TRzLabel.Create(nil);
    labs[i].Caption := colLst[i];
    labs[i].Left := i_r * StrToInt(search_width) + StrToInt(search_left);
    labs[i].Top := i_l * StrToInt(search_height) + StrToInt(search_top);
    labs[i].AutoSize := True;
    labs[i].Parent := RzPanel1;

    case colTyps[i] of
      ftString:
      begin
        objs[i] := TRzEdit.Create(nil);
        (objs[i] as TRzEdit).Width := StrToInt(search_tboxW);
        (objs[i] as TRzEdit).Left := i_r * StrToInt(search_width) + StrToInt(search_left) + StrToInt(search_labW);
        (objs[i] as TRzEdit).Top := i_l * StrToInt(search_height) + StrToInt(search_top);
        (objs[i] as TRzEdit).Parent := RzPanel1;
        (objs[i] as TRzEdit).OnChange := tboxChange;
      end;
      ftInteger, ftFloat:
      begin
        objs[i] := TRzNumericEdit.Create(nil);
        (objs[i] as TRzNumericEdit).Width := StrToInt(search_tboxW);
        (objs[i] as TRzNumericEdit).Left := i_r * StrToInt(search_width) + StrToInt(search_left) + StrToInt(search_labW);
        (objs[i] as TRzNumericEdit).Top := i_l * StrToInt(search_height) + StrToInt(search_top);
        (objs[i] as TRzNumericEdit).Parent := RzPanel1;
        (objs[i] as TRzNumericEdit).IntegersOnly := False;
        (objs[i] as TRzNumericEdit).DisplayFormat := ',0.##;(,0.##);,;(,)';
        (objs[i] as TRzNumericEdit).Text := '';
        (objs[i] as TRzNumericEdit).OnChange := tboxChange;
      end;
      ftDateTime:
      begin
        objs[i] := TRzDateTimeEdit.Create(nil);
        (objs[i] as TRzDateTimeEdit).Width := StrToInt(search_tboxW);
        (objs[i] as TRzDateTimeEdit).Left := i_r * StrToInt(search_width) + StrToInt(search_left) + StrToInt(search_labW);
        (objs[i] as TRzDateTimeEdit).Top := i_l * StrToInt(search_height) + StrToInt(search_top);
        (objs[i] as TRzDateTimeEdit).Parent := RzPanel1;
        (objs[i] as TRzDateTimeEdit).OnChange := tboxChange;
      end;
      else
	    begin
        objs[i] := TRzEdit.Create(nil);
        (objs[i] as TRzEdit).Width := StrToInt(search_tboxW);
        (objs[i] as TRzEdit).Left := i_r * StrToInt(search_width) + StrToInt(search_left) + StrToInt(search_labW);
        (objs[i] as TRzEdit).Top := i_l * StrToInt(search_height) + StrToInt(search_top);
        (objs[i] as TRzEdit).Parent := RzPanel1;
        (objs[i] as TRzEdit).OnChange := tboxChange;
      end;
    end;



    {
    tboxs[i] := TRzEdit.Create(nil);
    tboxs[i].Width := StrToInt(search_tboxW);
    tboxs[i].Left := i_r * StrToInt(search_width) + StrToInt(search_left) + StrToInt(search_labW);
    tboxs[i].Top := i_l * StrToInt(search_height) + StrToInt(search_top);
    tboxs[i].Parent := RzPanel1;
    tboxs[i].OnChange := tboxChange;
    }
  end;

  strSQL := '';
  RzRichEdit1.Text := '';

end;

procedure TFQry.FormCreate(Sender: TObject);
begin
  RzRichEdit1.Align := alClient;
  RzPanel1.Align := alClient;
end;

procedure TFQry.tboxChange(Sender: TObject);
var
  i: Integer;
  sVal : string;
begin
  strSQL := '';

  for i := 0 to Length(labs) - 1 do
  begin
    sVal := '';
    if objs[i] is TRzEdit then sVal := (objs[i] as TRzEdit).Text;
    if objs[i] is TRzNumericEdit then
    begin
      sVal := (objs[i] as TRzNumericEdit).Text;
    end;
    if objs[i] is TRzDateTimeEdit then sVal := (objs[i] as TRzDateTimeEdit).Text;

    if sVal <> '' then
    begin
      case colTyps[i] of
        ftString, ftDateTime:
        begin
          strSQL := strSQL + ' And ' + labs[i].Caption + ' Like ''' + sVal + '''';
        end;
        ftInteger, ftFloat:
        begin
          strSQL := strSQL + ' And ' + labs[i].Caption + '=' + sVal;
        end;
      end;    

    end;
  end;
  strSQL := 'select * from (' + sTabNam + ') A Where 1=1' + strSQL;
  RzRichEdit1.Text := strSQL;
end;

procedure TFQry.btnCancelClick(Sender: TObject);
begin
  strSQL := '';
  Close;
end;

procedure TFQry.btnQryClick(Sender: TObject);
begin
  strSQL := RzRichEdit1.Text;
  if strSQL = '' then strSQL := 'select * from (' + sTabNam + ') A';
  Close;
end;

end.
